package cc.codeshop.contentprovider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class PersonProvider extends ContentProvider {
	
	private static final String AUTHORITY = "cc.codeshop.provider";
//	private static final Uri CONTENT_URI = Uri.parse("content://cc.codeshop.provider");
	
	private UriMatcher uriMatcher;
	private DatabaseHelper dbHelper;

	public PersonProvider() {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		//列表的URI，如：如：content://cc.codeshop.provider/person/list
		uriMatcher.addURI(AUTHORITY, "person/list", 0);		//最后一个参数int code，用于在uriMatcher.match(uri)中返回值
		//单行的URI，如：content://cc.codeshop.provider/person/12
		uriMatcher.addURI(AUTHORITY, "person/#", 1);		//支持通配符：#表示数字，*表示任意文本
	}
	
	@Override
	public boolean onCreate() {
		Log.d("ccs", "onCreate");
		dbHelper = new DatabaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		Log.d("ccs", "query : " + uri);
		Cursor cursor = null;
		int code = uriMatcher.match(uri);
		if(code == 0) {		//查列表
			cursor = dbHelper.getReadableDatabase().query("person", projection, selection, selectionArgs, null, null, "person_id asc");			
		} else if(code == 1) {		//查单行（按ID查）
			String selection2 = "person_id=?";
			String[] selectionArgs2 = {uri.getLastPathSegment()};		//获取URI最后一个参数，如“content://cc.codeshop.provider/person/12”得到的是“12”
			cursor = dbHelper.getReadableDatabase().query("person", projection, selection2, selectionArgs2, null, null, null);
		}
		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		Log.d("ccs", "getType : " + uri);
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long rowId = dbHelper.getWritableDatabase().insert("person", null, values);
		Log.d("ccs", "insert : " + uri + ", rowId = " + rowId);
		Uri replyUri = ContentUris.withAppendedId(uri, rowId);
		return replyUri;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		Log.d("ccs", "delete : " + uri);
		int count = 0;
		int code = uriMatcher.match(uri);
		if(code == 0) {		//删多行(按条件删除，若没有条件则删除全部)
			count = dbHelper.getWritableDatabase().delete("person", selection, selectionArgs);			
		} else if(code == 1) {		//删一行（按ID删）
			String selection2 = "person_id=?";
			String[] selectionArgs2 = {uri.getLastPathSegment()};		//获取URI最后一个参数，如“content://cc.codeshop.provider/person/12”得到的是“12”
			count = dbHelper.getWritableDatabase().delete("person", selection2, selectionArgs2);
		}
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		Log.d("ccs", "update : " + uri);
		int count = 0;
		int code = uriMatcher.match(uri);
		if(code == 1) {		//更新某一行
			String selection2 = "person_id=?";
			String[] selectionArgs2 = {uri.getLastPathSegment()};		//获取URI最后一个参数，如“content://cc.codeshop.provider/person/12”得到的是“12”
			count = dbHelper.getWritableDatabase().update("person", values, selection2, selectionArgs2);
		}
		return 0;
	}
	
	static class DatabaseHelper extends SQLiteOpenHelper {
		
		private static final String DATABASE_NAME = "codeshop.db";
		private static final int DATABASE_VERSION = 2;

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE person(person_id INTEGER PRIMARY KEY, person_name TEXT, person_sex char(1));");
			Log.d("ccs", "创建数据库");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.d("ccs", "升级数据库版本：" + oldVersion + " -> " + newVersion);
			db.execSQL("DROP TABLE IF EXISTS person");
			onCreate(db);
		}
		
	}

}
